package com.vf.admin.miaodong.controller;

import com.vf.admin.client.service.AppUserDressupService;
import com.vf.admin.common.BaseController;
import com.vf.admin.common.Constants;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppHeadwear;
import com.vf.admin.miaodong.entity.AppMounts;
import com.vf.admin.miaodong.service.AfterHeadwearService;
import com.vf.admin.miaodong.service.AfterMountsService;
import com.vf.admin.utils.Base64Image.FileUtil;
import com.vf.admin.utils.StringEx;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 礼物商城管理  坐骑，头饰
 *
 * @author anxiang
 */
@Controller
@RequestMapping("/after/dressup")
public class AfterDressUpController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    AfterMountsService afterMountsService;

    @Autowired
    AfterHeadwearService afterHeadwearService;
    @Autowired
    AppUserDressupService appUserDressupService;

    /**
     * 获取坐骑管理列表
     *
     * @param splitPage
     * @param name      坐骑名称
     * @return
     */
    @RequestMapping("/list")
    public String list(SplitPageBean splitPage, String name) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("name", name);
            List<AppMounts> list = afterMountsService.findList(map, splitPage);
            request.setAttribute("list", list);
            request.setAttribute("splitPage", splitPage);
            request.setAttribute("name", name);
            return this.sucess("/web/after/dressup/mount/list");
        } catch (Exception e) {
            logger.error("获取坐骑管理列表异常", e);
            return this.error("系统繁忙");
        }
    }

    /**
     * 坐骑详情
     *
     * @param id id
     * @return
     */
    @RequestMapping("/detailMount")
    public String detailMount(Integer id) {
        try {
            AppMounts appMounts = afterMountsService.get(id);
            request.setAttribute("mount", appMounts);
            return this.sucess("/web/after/dressup/mount/edit");
        } catch (Exception e) {
            logger.error("头饰详情异常", e);
            return this.error("系统繁忙");
        }
    }

    /**
     * 删除头饰和坐骑
     * @param id
     * @param type
     * @return
     */
    @RequestMapping("/delHead")
    @ResponseBody
    public Result delHead(Integer id,Integer type){
        if(type.equals(1)) {
            afterHeadwearService.del(id);
        }else{
            afterMountsService.del(id);
        }
        return this.sucessJSON("操作成功");
    }

    /**
     * 保存坐骑信息
     *
     * @param appMounts 坐骑实体类
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public Result save(AppMounts appMounts) {
        try {
            if (StringEx.isNull(appMounts.getName())) {
                return this.errorJSON("坐骑名称不能为空");
            }
            if (StringEx.isNull(appMounts.getImgurl())) {
                return this.errorJSON("图片不能为空");
            }
            if (StringEx.isNull(appMounts.getMoney())) {
                return this.errorJSON("单价不能为空");
            }

            if (StringEx.isNull(appMounts.getDay())) {
                return this.errorJSON("天数不能为空");
            }
            if (appMounts.getId() != null) {
                AppMounts mounts = afterMountsService.get(appMounts.getId());
                mounts.setState(appMounts.getState() == null ? Constants.shiFou_STATE_FOU : appMounts.getState());
                mounts.setName(appMounts.getName());
                mounts.setMoney(appMounts.getMoney());
                mounts.setDay(appMounts.getDay());
                mounts.setEffects(appMounts.getEffects());
                String gifteffects = appMounts.getSvgaaddress().substring(appMounts.getSvgaaddress().indexOf("/test/") + 6, appMounts.getSvgaaddress().indexOf(".svga"));
                mounts.setGifteffects(gifteffects);
                mounts.setSvgaaddress(appMounts.getSvgaaddress());
                if (mounts.getImgurl().equals(appMounts.getImgurl())) {
                    mounts.setImgurl(appMounts.getImgurl());
                } else {
                    //处理图片
                    String newfilePath = "";
                    Map map = FileUtil.aliyunOSSUpload(appMounts.getImgurl(), "");
                    if (map.get("st").equals("0")) {
                        newfilePath = (String) map.get("data");
                    }
                    if (StringEx.isNotNull(newfilePath)) {
                        mounts.setImgurl(newfilePath);
                    }
                }
                mounts.setGuishu(appMounts.getGuishu());
                afterMountsService.save(mounts);
                //更新我的装扮图片信息
                appUserDressupService.updateZuoqiDate(mounts.getId(), 1, mounts.getImgurl(), appMounts.getName());
            } else {
                if(StringUtils.isNotBlank(appMounts.getSvgaaddress())) {
                    String gifteffects = appMounts.getSvgaaddress().substring(appMounts.getSvgaaddress().indexOf("/test/") + 6, appMounts.getSvgaaddress().indexOf(".svga"));
                    appMounts.setGifteffects(gifteffects);
                }

                appMounts.setCreatetime(new Date());
                //处理图片
                String newfilePath = "";
                Map map = FileUtil.aliyunOSSUpload(appMounts.getImgurl(), "");
                if (map.get("st").equals("0")) {
                    newfilePath = (String) map.get("data");
                }
                if (StringEx.isNotNull(newfilePath)) {
                    appMounts.setImgurl(newfilePath);
                }
                appMounts.setState(appMounts.getState() == null ? Constants.shiFou_STATE_FOU : appMounts.getState());
                afterMountsService.save(appMounts);
            }
            return this.sucessJSON("保存成功");
        } catch (Exception e) {
            logger.error("修改坐骑信息异常", e);
            return this.errorJSON("系统繁忙");
        }
    }

//===============================头饰操作=================

    /**
     * 头饰管理列表
     *
     * @param name 头饰名称
     * @return
     */
    @RequestMapping("/listHead")
    public String listHead(SplitPageBean splitPage, String name) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("name", name);
            List<AppHeadwear> list = afterHeadwearService.findList(map, splitPage);
            request.setAttribute("list", list);
            request.setAttribute("splitPage", splitPage);
            request.setAttribute("name", name);
            return this.sucess("/web/after/dressup/head/list");
        } catch (Exception e) {
            logger.error("获取头饰管理列表异常", e);
            return this.error("系统繁忙");
        }
    }


    /**
     * 头饰详情
     *
     * @param id id
     * @return
     */
    @RequestMapping("/detailHead")
    public String detail(Integer id) {
        try {
            AppHeadwear appHeadwear = afterHeadwearService.get(id);
            request.setAttribute("head", appHeadwear);
            return this.sucess("/web/after/dressup/head/edit");
        } catch (Exception e) {
            logger.error("头饰详情异常", e);
            return this.error("系统繁忙");
        }
    }


    /**
     * 编辑头饰信息
     *
     * @param appHeadwear 头饰实体类
     * @return
     */
    @RequestMapping("/saveHead")
    @ResponseBody
    public Result saveHead(AppHeadwear appHeadwear) {
        try {
            if (StringEx.isNull(appHeadwear.getName())) {
                return this.errorJSON("头饰名称不能为空");
            }
            if (StringEx.isNull(appHeadwear.getImgurl())) {
                return this.errorJSON("图片不能为空");
            }
            if (StringEx.isNull(appHeadwear.getMoney())) {
                return this.errorJSON("单价不能为空");
            }
            if (StringEx.isNull(appHeadwear.getDay())) {
                return this.errorJSON("天数不能为空");
            }
            if (StringEx.isNull(appHeadwear.getMold())) {
                return this.errorJSON("类型不能为空");
            }
            if (appHeadwear.getId() != null) {
                AppHeadwear appHeadwear1 = afterHeadwearService.get(appHeadwear.getId());
                appHeadwear1.setMold(appHeadwear.getMold());
                appHeadwear1.setName(appHeadwear.getName());
                appHeadwear1.setMoney(appHeadwear.getMoney());
                appHeadwear1.setDay(appHeadwear.getDay());
                appHeadwear1.setGuishu(appHeadwear.getGuishu());
                if (appHeadwear1.getImgurl().equals(appHeadwear.getImgurl())) {
                    appHeadwear1.setImgurl(appHeadwear.getImgurl());
                } else {
                    appHeadwear1.setImgurl(appHeadwear.getImgurl());
                }
                appHeadwear1.setSvgaAddress(appHeadwear.getSvgaAddress());
                appHeadwear1.setState(appHeadwear.getState() == null ? Constants.shiFou_STATE_FOU : appHeadwear.getState());
                afterHeadwearService.save(appHeadwear1);
                //更新我的装扮图片信息
                appUserDressupService.updateZuoqiDate(appHeadwear1.getId(), 2, appHeadwear1.getImgurl(), appHeadwear1.getName());
            } else {
                appHeadwear.setCreatetime(new Date());
                appHeadwear.setMold(appHeadwear.getMold());
                appHeadwear.setState(appHeadwear.getState() == null ? Constants.shiFou_STATE_FOU : appHeadwear.getState());
                afterHeadwearService.save(appHeadwear);
            }
            return this.sucessJSON("保存成功");
        } catch (Exception e) {
            logger.error("编辑头饰信息异常", e);
            return this.errorJSON("系统繁忙");
        }
    }


}
